# encoding: utf-8
# author: 侯佳涛
# file_name: 2.lxml_test
# create_time: 2021/5/23, 上午11:44
from lxml import etree

text = ''' 
<div> 
  <ul> 
    <li class="item-1">
      <a href="link1.html"></a>
    </li> 
    <li class="item-1">
      <a href="link2.html">second item</a>
    </li> 
    <li class="item-inactive">
      <a href="link3.html">third item</a>
    </li> 
    <li class="item-1">
      <a href="link4.html">fourth item</a>
    </li> 
    <li class="item-0">
      a href="link5.html">fifth item</a>
  </ul> 
</div>
'''

# -----------------------------------------------
# # etree.HTML()返回的是Element对象
# html = etree.HTML(text)

# # 获取href的列表和title的列表
# # etree.HTML().xpath()返回的是Element对象列表或者属性列表或者文本列表
# href_list = html.xpath('//a/@href')
# title_list = html.xpath('//a/text()')
#
# print(href_list)
# print(title_list)
#
# # 组装成字典
# result_dict = {}
#
# for title, href in zip(title_list, href_list):
#     result_dict[title] = href
#
# print(result_dict)
#
# a_el_list = html.xpath('//a')
# print(a_el_list)
#
# for el in a_el_list:
#     print(el.xpath('./text()'))

# ----------------------------------------------- 从爬去的数据中提取href, title，存储为字典的正确方法
# # 获取文本的Element对象
# html = etree.HTML(text)
#
# # 获取目标所在标签的Element对象列表
# el_list = html.xpath('//a[contains(@href, link)]')
# # print(el_list)
# # for el in el_list:
# #     print(el.xpath('./text()'))
#
# # 提取信息整理信息
# result_dict = {}
#
# for el in el_list:
#     try:
#         el_href = el.xpath('./@href')[0]
#         el_title = el.xpath('.//text()')[0]
#         # el_title2 = el.xpath('text()')  # 和el_title等价
#         # print(el_href, el_title, el_title2)
#         result_dict[el_title] = el_href
#     except:
#         continue
#
# print(result_dict)

# ---------------------------------------------- etree.tostring()
# 这个方法会自动补全html语法中缺失的标签，比如text中</ul>前缺少</li>，通过这个方法会自动补全</li>标签，使text成为正确的html字符串
# 注意：不是tostring方法补全的
html = etree.HTML(text)
print(html)

print(etree.tostring(html))


